<?php
$loader->requireOnce('includes/EwhExport/EwhExportException.class.php');
/**
 * Processor for doing export drug
 * @package modules.dept_pharmacy.includes.EwhExport
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class EwhExportProc {
    private $controller;
    private $logger ;
    function __construct($controller) {
        $this->controller = $controller;
        $this->logger = LoggerManager::getLogger('modules.dept_pharmacy.includes.EwhExport.EwhExportProc');
    }

    public function createAcceptedExportTicket($data) {
        $phaEwhExticket = Celini::newOrdo('PhaEwhExticket');
        $data['enum_status'] = EnumerationConstant::PHA_REQ_STATUS_ACCEPTED_KEY;
        $result = $phaEwhExticket->createExTicket($data);
        if ($result == null || !isset($result['id']) || $result['id'] == '') {
            //creating fail
            throw new EwhExportException("create exTicket fail",
            EwhErrorCode::EXP_CREAT_EXTICKET_FAIL);
             
        }   else {
            //update seq
            $phaSequences = Celini::newOrdo('PhaSequences');
            $phaSequences->incExTicketSeq();
            //update status of request ticket to accepted
            $phaReqExticketObj = Celini::newOrdo('PhaReqExticket');
            $phaReqExticketObj->setStatus($data['pha_req_exticket_id'],
            EnumerationConstant::PHA_REQ_STATUS_ACCEPTED_KEY
            );

        }
        return $result;
    }

    public function saveAcceptedExportTicket($expTicketId, $data) {
        $phaEwhExticket = Celini::newOrdo('PhaEwhExticket');
        if (!$phaEwhExticket->isExist($expTicketId)) {
            throw new EwhExportException("exTicket not exist [".$expTicketId . "]",
            EwhErrorCode::EXP_EXTICKET_NOT_EXIST);
        }
        $data['enum_status'] = EnumerationConstant::PHA_REQ_STATUS_ACCEPTED_KEY;

        $phaEwhExticket->setup($expTicketId);
        $phaEwhExticket->setData($data);
        $phaEwhExticket->persist();

        //update status of request ticket to accepted
        $phaReqExticketObj = Celini::newOrdo('PhaReqExticket');
        $phaReqExticketObj->setStatus($data['pha_req_exticket_id'],
        EnumerationConstant::PHA_REQ_STATUS_ACCEPTED_KEY);
         

        $result = array(
        'code'=>$phaEwhExticket->get('exticket_code'),
        'id' =>$phaEwhExticket->get_id()
        );

        return $result;
    }

    public function rejectExpTicket($data) {
        $phaEwhExticket = Celini::newOrdo('PhaEwhExticket');
        $data['enum_status'] = EnumerationConstant::PHA_REQ_STATUS_REJECTED_KEY;
        $result = $phaEwhExticket->createRejectedExTicket($data);
        if ($result == null || !isset($result['id']) || $result['id'] == '') {
            //creating fail
            throw new EwhExportException("reject exTicket fail",
            EwhErrorCode::EXP_REJECT_EXTICKET_FAIL);
             
        }   else {
            //update seq
            //update status of request ticket to accepted
            $phaReqExticketObj = Celini::newOrdo('PhaReqExticket');
            $phaReqExticketObj->setStatus($data['pha_req_exticket_id'],
            EnumerationConstant::PHA_REQ_STATUS_REJECTED_KEY);

        }
        return $result;
    }

    public function saveRejectedExpTicket($expTicketId, $data) {
        $phaEwhExticket = Celini::newOrdo('PhaEwhExticket');
        if (!$phaEwhExticket->isExist($expTicketId)) {
            throw new EwhExportException("exTicket not exist [".$expTicketId . "]",
            EwhErrorCode::EXP_EXTICKET_NOT_EXIST);
        }
        $data['enum_status'] = EnumerationConstant::PHA_REQ_STATUS_REJECTED_KEY;
        $phaEwhExticket->setup($expTicketId);
        $phaEwhExticket->setData($data);
        $phaEwhExticket->persist();

        //update status of request ticket to accepted
        $phaReqExticketObj = Celini::newOrdo('PhaReqExticket');
        $phaReqExticketObj->setStatus($data['pha_req_exticket_id'],
        EnumerationConstant::PHA_REQ_STATUS_REJECTED_KEY);

        $result = array(
        'code'=>$phaEwhExticket->get('exticket_code'),
        'id' =>$phaEwhExticket->get_id()
        );

        return $result;
    }

    public function listReqExticketDetail($reqExTicketId) {
        $PhaReqExticketDetailObj = Celini::newOrdo('PhaReqExticketDetail');
        $arrReqExpTicketDetailInfo =  $PhaReqExticketDetailObj->listReqExticketDetail($reqExTicketId);
        $this->decorateItemsOfReqExticketDetail($arrReqExpTicketDetailInfo);

        return $arrReqExpTicketDetailInfo;
    }
    
    public function listApprovedExticketDetail($reqExTicketId) {
        $PhaReqExticketDetailObj = Celini::newOrdo('PhaReqExticketDetail');
        $arrReqExpTicketDetailInfo =  $PhaReqExticketDetailObj->listAprrovedExticketDetail($reqExTicketId);
        $this->decorateItemsOfApprovedExticketDetail($arrReqExpTicketDetailInfo);

        return $arrReqExpTicketDetailInfo;
    }

    public function getReqExticketDetail($reqExTicketDetailId) {
        $PhaReqExticketDetailObj = Celini::newOrdo('PhaReqExticketDetail');
        $reqExpTicketDetailInfo =  $PhaReqExticketDetailObj->getReqExticketDetail($reqExTicketDetailId);
        $this->decorateAReqExticketDetail($reqExpTicketDetailInfo);

        return $reqExpTicketDetailInfo;
    }

    private function decorateAReqExticketDetail(&$item){
        $arrUnit = Utils::getEnumArrayKey(EnumerationConstant::PHA_IMSE_UNIT);
        $item['quantity_formatted'] = L10nHelper::renderNumber($item['quantity']);
        if (isset($arrUnit[$item['enum_unit_import_sell']])) {
            $item['enum_unit_import_sell_text'] = $arrUnit[$item['enum_unit_import_sell']];

        } else {
            $item['enum_unit_import_sell_text'] = '';
        }

    }

    private function decorateItemsOfReqExticketDetail(&$data = array()){
        $arrUnit = Utils::getEnumArrayKey(EnumerationConstant::PHA_IMSE_UNIT);
        foreach ($data as &$item) {
            $item['quantity_formatted'] = L10nHelper::renderNumber($item['quantity']);
            if (isset($arrUnit[$item['enum_unit_import_sell']])) {
                $item['enum_unit_import_sell_text'] = $arrUnit[$item['enum_unit_import_sell']];

            } else {
                $item['enum_unit_import_sell_text'] = '';
            }
        }

    }
    
    private function decorateItemsOfApprovedExticketDetail(&$data = array()){
        $arrUnit = Utils::getEnumArrayKey(EnumerationConstant::PHA_IMSE_UNIT);
        foreach ($data as &$item) {
            $item['quantity_formatted'] = L10nHelper::renderNumber($item['quantity']);
            $item['approve_quantity_formatted'] = L10nHelper::renderNumber($item['approve_quantity']);
            
            if (isset($arrUnit[$item['enum_unit_import_sell']])) {
                $item['enum_unit_import_sell_text'] = $arrUnit[$item['enum_unit_import_sell']];

            } else {
                $item['enum_unit_import_sell_text'] = '';
            }
        }

    }

    public function getExTicketOfGivenReq($reqExTicketId) {
        $phaEwhExticketObj = Celini::newOrdo('PhaEwhExticket');
        $exTicket =  $phaEwhExticketObj->getExpTicketOfGivenReq($reqExTicketId);
        $this->decorateExTicket($exTicket);
        return $exTicket;

    }
    private function decorateExTicket(&$item){
        $item['app_date_formatted'] = L10nHelper::renderDateTime($item['app_date']);

    }
    
    public function rollBackApprovedExticket($expTicketId) {
        
        //get list of exp detail        
        $phaEwhExticketDetailObj = Celini::newOrdo('PhaEwhExticketDetail');
        $phaEwhPickDrugObj = Celini::newOrdo('PhaEwhPickDrug');
        $listDt = $phaEwhExticketDetailObj->listItemOfGivenExTicket($expTicketId);
        
        foreach ($listDt as $detailItem) {
        	$listPickedDugs = $phaEwhPickDrugObj->listItemOfGivenExticketDetail($detailItem['pha_ewh_exticket_detail_id']);
        	foreach ($listPickedDugs as $pickedItem) {
        	    //pha_ewh_pick_drug_id, pha_ewh_imticket_detail_id, quantity
        	    if ($pickedItem['quantity'] > 0) {
            	    $phaEwhImticketDetailObj = Celini::newOrdo('PhaEwhImticketDetail', $pickedItem['pha_ewh_imticket_detail_id']);
            	    $q = $phaEwhImticketDetailObj->get('remain_quantity');
            	    $data = array('remain_quantity'=> $q + $pickedItem['quantity']);
            	    $phaEwhImticketDetailObj->setData($data);
            	    $phaEwhImticketDetailObj->persist();
            	    //delete this picked item   
        	    }
        	         	            	    
        	}
        	$phaEwhPickDrugObj->deleteItemByEDId($detailItem['pha_ewh_exticket_detail_id']);
        	//delete this drug detail item  
        	$phaEwhExticketDetailObj->deleteItem($detailItem['pha_ewh_exticket_detail_id']);     	
        	
        }
        
        
    }


}
?>